﻿using Comain;
using Comain.DTO;
using Common;
using IService;
using IService.Rempos;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace WdbApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class GoodsController : ControllerBase
    {
        private readonly IGoodsService goodss;
        private readonly ILogger<GoodsController> logger;

        public GoodsController(IGoodsService goodss,ILogger<GoodsController> logger)
        {
            this.goodss = goodss;
            this.logger = logger;
        }

        [HttpPost("AddGood")]
        public async Task<APIResult<bool>> AddGood(T_Goods goodsd)
        {
            var result = await goodss.AddGood(goodsd);
            return new APIResult<bool>("")
            {
                Data = result,
                Message = "添加成功",
                Isucess = true,
                Code = ResultCode.OK,
            };
        }
        /// <summary>
        /// 获取所有商品品牌信息
        /// </summary>
        /// <returns>返回满足条件的数据</returns>
        [HttpGet("ShowGoods")]
        public APIResult<IQueryable<Goods>> ShowGoods()
        {
            try
            {
                var result = goodss.ShowGoods();
                return new APIResult<IQueryable<Goods>>("")
                {
                    Data = result,
                    Message = "显示成功",
                    Isucess = true,
                    Code = ResultCode.OK,
                };
            }
            catch (Exception ex)
            {
                logger.LogError("获取所有商品品牌信息异常" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取所有商品分类信息
        /// </summary>
        /// <param name="perentid">父级分类编号（作为条件进行筛选）</param>
        /// <returns>返回所有商品分类信息</returns>
        [HttpGet("ShowGoodsTypes")]
        public APIResult<List<Cascaderitrm>> ShowGoodsTypes(int perentid)
        {
            var result = goodss.ShowGoodsTypes(perentid);
            return new APIResult<List<Cascaderitrm>>("")
            {
                Data = result,
                Message = "显示成功",
                Isucess = true,
                Code = ResultCode.OK,
            };
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("AddImg")]
        public IActionResult AddImg(IFormFile file)
        {
            var shang = "wwwroot/Img/" + file.FileName;
            using (var fs = new FileStream(shang, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            var path = "http://localhost:5269/Img/" + file.FileName;
            return Ok(path);
        }
        /// <summary>
        /// 显示商品信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("Show")]
        public async Task<APIResult<PageComm<List<GoodsDTO>>>> Show(string? name, int? goodid, DateTime? time, int pageIndex, int pageSize)
        {
            var result = await goodss.Show(name, goodid, time, pageIndex, pageSize);
            return new APIResult<PageComm<List<GoodsDTO>>>("")
            {
                Data = result,
                Message = "显示成功",
                Isucess = true,
                Code = ResultCode.OK,
            };
        }
        /// <summary>
        /// 修改商品
        /// </summary>
        /// <param name="goodsds"></param>
        /// <returns></returns>
        [HttpPut("UpdateGood")]
        public async Task<APIResult<bool>> UpdateGood(T_Goods goodsds)
        {
            var result = await goodss.UpdateGood(goodsds);
            return new APIResult<bool>("")
            {
                Data = result,
                Message = "修改成功",
                Isucess = true,
                Code = ResultCode.OK,
            };
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("GetT_Goods")]
        public async Task<APIResult<T_Goods>> GetT_Goods(int id)
        {
            var result = await goodss.GetT_Goods(id);
            return new APIResult<T_Goods>("")
            {
                Data = result,
                Message = "反填",
                Isucess = true,
                Code = ResultCode.OK
            };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete("DeleteAsync")]
        public async Task<APIResult<int>> DeleteAsync(int id)
        {
            var result = await goodss.DeleteAsync(id);
            return new APIResult<int>("")
            {
                Data = result,
                Message = "删除chengg",
                Isucess = true,
                Code = ResultCode.OK
            };
        }
    }
}
